//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.graphreport.util;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.ht.query.QueryGenerator;
import com.ht.modules.system.online.cgform.util.CgformUtil;
import com.ht.modules.system.online.cgform.util.OnlineDbHandler;

public class GraphreportUtil extends CgformUtil {
    public static final String SQL_PARENT_SELECT_BEGIN = "SELECT * FROM (";
    public static final String SQL_PARENT_SELECT_END = ") temp_table WHERE 1=1";

    public GraphreportUtil() {
    }

    public static List<Map<String, Object>> getParameterMap(String paramString) {
        ArrayList returnList = new ArrayList<>();

        String params;
        try {
            params = URLDecoder.decode(paramString, "UTF-8");
        } catch (Exception var6) {
            return null;
        }

        JSONArray array = JSONArray.parseArray(params);
        if (array == null) {
            return null;
        } else {
            for(int i = 0; i < array.size(); ++i) {
                JSONObject object = array.getJSONObject(i);
                returnList.add(object);
            }

            return returnList;
        }
    }

    public static String getSplicingConditionSql(String sql, String paramString) {
        return getSplicingConditionSql(sql, getParameterMap(paramString));
    }

    public static String getSplicingConditionSql(String sql, List<Map<String, Object>> params) {
        if (params == null) {
            return sql;
        } else {
            StringBuffer sb = new StringBuffer();
            sb.append("SELECT * FROM (");
            if (sql.trim().lastIndexOf(";") == sql.length() - 1) {
                sql = sql.substring(0, sql.length() - 1);
            }

            sb.append(sql);
            sb.append(") temp_table WHERE 1=1");

            try {
                Iterator var3 = params.iterator();

                while(var3.hasNext()) {
                    Map<String, Object> map = (Map)var3.next();
                    String value = map.get("value").toString();
                    String fieldName = map.get("fieldName").toString();
                    String fieldType = map.get("fieldType").toString();
                    String searchMode = map.get("searchMode").toString();
                    if ("single".equalsIgnoreCase(searchMode)) {
                        if (!StringUtils.isEmpty(value)) {
                            boolean isString = !OnlineDbHandler.isNumField(fieldType);
                            String fieldCondition = QueryGenerator.getSingleQueryConditionSql(fieldName.toLowerCase(), "", value, isString);
                            sb.append(" AND " + fieldCondition);
                        }
                    } else {
                        List<Object> values = JSONArray.parseArray(value);
                        Object _begin = values.get(0);
                        Object _end = values.get(1);
                        if (_begin != null) {
                            sb.append(" AND " + fieldName + ">=");
                            if (OnlineDbHandler.isNumField(fieldType)) {
                                sb.append(_begin.toString());
                            } else {
                                sb.append("'" + _begin.toString() + "'");
                            }
                        }

                        if (_end != null) {
                            sb.append(" AND " + fieldName + "<=");
                            if (OnlineDbHandler.isNumField(fieldType)) {
                                sb.append(_end.toString());
                            } else {
                                sb.append("'" + _end.toString() + "'");
                            }
                        }
                    }
                }

                return sb.toString();
            } catch (Exception var12) {
                return sql;
            }
        }
    }
}
